#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/4/20 14:28
# @Author : 闫峰
# @File    : check_bundle_list.py
from third_services.shopee.bundle_deal.bundle_deal import InterfaceBundleDeal
from utils.mysqlUtils.db_pool import Database
from utils.logging_tool.log_control import ERROR, WARNING, INFO

db = Database('jdwart_database')


class TestCheckBundleList:

    def test_check_bundle_list(self, ordersourceid):
        page_no = 1
        bundle_deal_id_list = []
        while True:
            res = InterfaceBundleDeal().get_bundle_deal_list(ordersourceid, page_no)
            bundle_deal_list = res.get('response').get('bundle_deal_list')
            for bundle_deal in bundle_deal_list:
                sql1 = """SELECT id FROM test_zhcxkj_center_promotion.t_shopee_bundle_deal WHERE platform_bundle_deal_id=%s;"""
                res1 = db.fetch_all(sql1, (bundle_deal['bundle_deal_id'],))
                if res1:
                    bundle_deal_id_list.append((bundle_deal['bundle_deal_id'], res1[0]['id']))
                else:
                    ERROR.logger.error(
                        f"渠道id {ordersourceid} 下的套装活动id {bundle_deal['bundle_deal_id']} 未同步到数据库t_shopee_bundle_deal")
            if not res['response']['more']:
                break
            else:
                page_no += 1
        return bundle_deal_id_list

    def test_check_bundle_deal_item(self, ordersourceid):
        bundle_deal_id_list = self.test_check_bundle_list(ordersourceid)
        for bundle_deal_id in bundle_deal_id_list:
            res1 = InterfaceBundleDeal().get_bundle_deal_item(ordersourceid, bundle_deal_id[0])
            INFO.logger.info(f"套装活动id {bundle_deal_id[0]} 绑定的产品信息如下：")
            INFO.logger.info(f"套装活动id {bundle_deal_id[0]} 绑定的产品总数为：{res1.get('response').get('total_count')}")
            if res1.get('response').get('total_count'):
                item_list = res1.get('response').get('item_list')
                # WARNING.logger.warning(item_list)
                for item_id in item_list:
                    # print(item_id)
                    sql2 = """SELECT enabled_status FROM test_zhcxkj_center_promotion.t_shopee_bundle_deal_product WHERE bundle_deal_id=%s AND item_id=%s;"""
                    res2 = db.fetch_all(sql2, (bundle_deal_id[1], item_id['item_id']))
                    if res2:
                        if res2[0]['enabled_status'] == item_id['status']:
                            pass
                        else:
                            ERROR.logger.error(
                                f"套装活动id {bundle_deal_id[0]} 下的产品 {item_id['item_id']} 在t_shopee_bundle_deal_product数据库的enabled_status 状态不对 ")
                    else:
                        ERROR.logger.error(
                            f"套装活动id {bundle_deal_id[0]} 下的产品 {item_id['item_id']} 在t_shopee_bundle_deal_product数据库查询不到")
            else:
                INFO.logger.info(f"套装活动id {bundle_deal_id[0]} 产品信息验证完成：")
                continue
            INFO.logger.info(f"套装活动id {bundle_deal_id[0]} 产品信息验证完成：")


if __name__ == '__main__':
    TestCheckBundleList().test_check_bundle_list(14257)
    TestCheckBundleList().test_check_bundle_list(14257,208339592)
